package com.elitel.frame.base.controller;

import com.elitel.common.elasticSearch.ESClient;
import com.elitel.frame.base.component.quartz.SearchServiceTask;
import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.CfgExtsearchService;
import com.elitel.frame.base.service.CfgFieldService;
import com.elitel.frame.base.service.CfgTableService;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgExtsearch;
import com.elitel.frame.main.entity.CfgField;
import com.elitel.frame.main.entity.CfgTable;
import com.elitel.frame.main.entity.ext.CfgExtsearchExt;
import com.elitel.frame.main.entity.vo.FullTextSearchVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * show 全文检索服务控制器
 * by date 2018/03/12 guoyanfei
 */
@Controller
@RequestMapping("/system/fullTextSearch")
public class FulltextSearchController {
    private static final Logger logger = LoggerFactory.getLogger(FulltextSearchController.class);

    @Autowired
    private CfgDbService cfgDbService;
    @Autowired
    private CfgTableService cfgTableService;
    @Autowired
    private CfgExtsearchService cfgExtsearchService;
    @Autowired
    private CfgFieldService cfgFieldService;
    @Autowired
    private SearchServiceTask searchServiceTask;

    /**
     * show 查询所有配置的数据库信息
     * by date 2018/03/12
     * @return CfgDb实体List集合
     * @author guoyanfei
     */
    @RequestMapping("/getAlldatabase")
    @ResponseBody
    public List<CfgDb> getAlldatabase(){
        List<CfgDb> cfgDbList = new ArrayList<>();
        try {
            cfgDbList = cfgDbService.getAllListCfgDb();
        }catch (Exception e){
           logger.error(e.getMessage(),e);
        }

        return cfgDbList;
    }

    /**
     * show 通过数据库标识(dbkey)查询对应数据表信息
     * by date 2018/03/12
     * @param request dbkey数据库标识
     * @return CfgTable实体list集合
     * @author guoyanfei
     */
    @RequestMapping("/getTablesBydbkey")
    @ResponseBody
    public List<CfgTable> getTablesBydbkey(HttpServletRequest request){
        List<CfgTable> cfgTableList = new ArrayList<>();
        try{
            String dbkey = request.getParameter("dbkey");
            cfgTableList = cfgTableService.getListByConnAndtableType(dbkey,1);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return cfgTableList;
    }

    /**
     * show 通过表标识查询字段类型为String的字段信息
     * by date 2018/03/13
     * @param request dtConfigId 表标识
     * @return CfgField实体list集合
     * @author guoyanfei
     */
    @RequestMapping("/getFieldInfo")
    @ResponseBody
    public List<CfgField> getFieldInfo(HttpServletRequest request){
        List<CfgField> cfgFields = new ArrayList<>();
        try{
            String dtConfigId = request.getParameter("dtConfigId");
            cfgFields = cfgExtsearchService.getFieldBydtconfig(dtConfigId);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return cfgFields;
    }

    /**
     * show 查询服务ID是否存在
     * by date 2018/03/14
     * @param request guid服务ID
     * @return true或false
     * @author guoyanfei
     */
    @RequestMapping("/isExtServiceID")
    @ResponseBody
    public Boolean isExtServiceID(HttpServletRequest request){
        Boolean result = false;
        try{
            String guid = request.getParameter("guid");
            CfgExtsearch cfgExtsearch = cfgExtsearchService.getSearchByguid(guid);
            if(cfgExtsearch != null){
                result = true;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * show 新增全文检索服务配置信息
     * created on date 2018/03/14
     * @return 新增结果
     * @author guoyanfei
     */
    @RequestMapping("/addSearchInfo")
    @ResponseBody
    public Integer addSearchInfo(@RequestBody CfgExtsearchExt cfgExtsearchExt){
        Integer result = 0;
        try{
            result = cfgExtsearchService.addSearchInfo(cfgExtsearchExt);

            //启动任务调度
            searchServiceTask.startSearchServiceScheduler(cfgExtsearchExt.getGuid());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * show 通过服务ID查询全文检索服务基本信息
     * created by date 2018/03/14
     * @param request guid 服务ID
     * @return CfgExtsearch 实体
     * @author guoyanfei
     */
    @RequestMapping("/selectCfgExtsearch")
    @ResponseBody
    public CfgExtsearch selectCfgExtsearch(HttpServletRequest request){
        CfgExtsearch cfgExtsearch = new CfgExtsearch();
        try {
            String guid = request.getParameter("guid");
            cfgExtsearch = cfgExtsearchService.getSearchByguid(guid);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return  cfgExtsearch;
    }

    /**
     * show 通过服务ID查询全文检索服务配置表信息
     * created by date 2018/03/14
     * @param request guid 服务id
     * @return FullTextSearchVo 实体 list集合
     * @author guoyanfei
     */
    @RequestMapping("/selectFullsearch")
    @ResponseBody
    public  List<FullTextSearchVo> selectFullsearch(HttpServletRequest request){
        List<FullTextSearchVo> fullTextSearchVos = new ArrayList<>();
        try {
            String guid = request.getParameter("guid");
            fullTextSearchVos = cfgExtsearchService.getBasesearchInfoByserviceId(guid);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return fullTextSearchVos;
    }

    /**
     * show 更新全文检索服务基本信息及配置信息
     * created by date 2018/03/14
     * @param cfgExtsearchExt 实体
     * @return 更新条数
     * @author guoyanfei
     *
     */
    @RequestMapping("/updateFullsearch")
    @ResponseBody
    public  Integer updateFullsearch(@RequestBody CfgExtsearchExt cfgExtsearchExt){
        Integer result = 0;
        try{
            result = cfgExtsearchService.updateSearchInfo(cfgExtsearchExt);

            //修改任务调度时间
            searchServiceTask.updateSchedulerTime(cfgExtsearchExt.getGuid(),cfgExtsearchExt.getCronexpression());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return  result;
    }

    /**
     * show 通过表标识查询所有表字段信息
     * created on 2018/03/16
     * @param request dtconfigid 表标识
     * @return CfgField实体list集合
     * @author guoyanfei
     */
    @RequestMapping("/getAllField")
    @ResponseBody
    public List<CfgField> getAllField(HttpServletRequest request){
        List<CfgField> cfgFields = new ArrayList<>();
        try{
            String dtconfigid = request.getParameter("dtconfigid");
            cfgFields = cfgFieldService.getAllField(dtconfigid);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return cfgFields;
    }

    /**
     * show 随机生成一个GUID
     * created on 2018/03/16
     * @return UUID
     * @author guoyanfei
     */
    @RequestMapping("/generateUuid")
    @ResponseBody
    public String generateUuid(){
        return UUID.randomUUID().toString();
    }

}
